﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace Sublimation
{
    public class EnemyUnitPriest : EnemyUnit
    {
        new protected int NUMBER_OF_FRAMES_TO_PLAY = 4;
        new protected const int FRAMES_PER_SECOND = 10;
        protected float range = 120;
        protected EnemyUnit target; 
        protected float rangeSpeed = 500;
        protected float rangedAttackTimerMillis = 0;

        public EnemyUnitPriest(WorldProperties worldProperties, EnemyKind kind, int spawnPoint)
            : base(worldProperties, kind, spawnPoint)
        {
        }

        public EnemyUnitPriest(WorldProperties worldProperties, EnemyKind kind)
            : base(worldProperties, kind)
        {
        }

        protected override void SetupBehaviours()
        {
            base.SetupBehaviours();

            for (int i = 0; i < behaviours.Count; i++)
            {
                if (typeof(Behaviours.EnemyUnitGoalBehaviour) == behaviours[i].GetType())
                {
                    behaviours.Remove(behaviours[i]);
                }
            }
        }

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
            rangedAttackTimerMillis += gameTime.ElapsedGameTime.Milliseconds;

            getTarget();

            if (rangedAttackTimerMillis > meleeAttackDelay)
            {
                rangedAttackTimerMillis = 0;
                if (target != null)
                {
                    new HealingBall(worldProperties, target, Position, MeleeDamage);
                }
                else
                {
                    attacking = false;
                }
            }
        }

        public void getTarget()
        {
            target = null;
            float lifeMissingMax = 0;
            float lifeMissing;
            foreach (EnemyUnit enemy in worldProperties.Enemies)
            {
                lifeMissing = enemy.MaxLife - enemy.Life;
                if (lifeMissing > MeleeDamage && Vector2.Distance(enemy.Position, position) < range && enemy != this)
                {
                    if (target == null || lifeMissing > lifeMissingMax)
                    {
                        target = enemy;
                        lifeMissingMax = lifeMissing;
                    }
                }
            }

        }

        public override void DoMovement(Microsoft.Xna.Framework.GameTime gameTime)
        {
            if (target != null)
            {
                attacking = true;
            }
            else
            {
                base.DoMovement(gameTime);
            }
        }


        protected override void LoadContent()
        {
            base.LoadContent();
            Origin = new Vector2(17, 30);
            textureAnimator = new AnimatedSprite.AnimatedTexture(Origin, Rotation, Scale, 1.0f);
            textureAnimator.Load(Game.Content, "Sprites\\doctor", NUMBER_OF_FRAMES_TO_PLAY, FRAMES_PER_SECOND);
            textureAttack = new AnimatedSprite.AnimatedTexture(new Vector2(16, 35), Rotation, Scale * 1f, 1.0f);
            textureAttack.Load(Game.Content, "Sprites\\doctor_attack", 8, 10);
            Scale = 1f;
        }


        public override void Kill()
        {
            base.Kill();
            if (Life <= 0) new Animation(worldProperties, this, 1f, 4, 10, "Sprites\\doctor_dying");
        }
    }
}
